<?php
/*! \brief
 *  This is WFL10N abstract class
 * \author William Su
 */
abstract class AL10N {
    /**
     * @var WF
     */
    protected $objFramework;
    /**
     * @var WFWidgetPoolFactory
     */
    protected $objWidgetPoolFactory;
    /**
     * @var WFLangFileManager
     */
    protected $objLangFileManager;
    /**
     * @var WFConfig
     */
    protected $objConfig;
    protected $objSimpleXml;
    protected $strProductFolder;
    /**
     * @var WFDomFactory
     */
    protected $objDomFactory;
    protected static $instance;
    protected $strLangType;
    protected $intUserGenerated;
    public function __construct($objFramework, $strProductFolder, $strLangType, $intUserGenerated){
        $this->objFramework = $objFramework;
        $this->objLangFileManager = $this->objFramework->getLangFileFactory($objFramework)->getLangFileManager();
        $this->objConfig = $this->objFramework->getConfigFactory($objFramework)->getConfig();
        $this->objDomFactory = $this->objFramework->getDomFactory($objFramework);
        //$this->objSimpleXml = $this->objFramework
        $this->objWidgetPoolFactory = $this->objFramework->getWidgetPoolFactory($objFramework);
        $this->strProductFolder = $strProductFolder;
        $this->strLangType = $strLangType;
        $this->intUserGenerated = $intUserGenerated;
    }
    public function setObjWidgetPoolFactory($objWidgetPoolFactory){
        $this->objWidgetPoolFactory = $objWidgetPoolFactory;
    }
    public function getAssocLabel(){
        $strLang = $this->objConfig->getCurrentLang();
        $strPathL10NFile = $this->objLangFileManager->getPathLangFile($strLang, $this->strProductFolder, $this->strLangType, $this->intUserGenerated);
        
        // use self::$instance to avoid recalculate L10N array already been calculated
        $antiDuplicatekey = $this->strProductFolder.$this->strLangType;
        if(!isset(self::$instance['arrTranslations'][$antiDuplicatekey])){
            $elTranslations = new SimpleXMLElement($strPathL10NFile, null, true);
            $objSimpleXMLElementL10N = $elTranslations->xpath('//string');
            $arrSimpleXMLElementL10N = (array)$objSimpleXMLElementL10N;
            if(is_array($arrSimpleXMLElementL10N) && $arrSimpleXMLElementL10N){
                foreach($arrSimpleXMLElementL10N as $strSimpleXMLElementL10N){
                    $key = (string)$strSimpleXMLElementL10N->attributes()->id;
                    $value = (string)$strSimpleXMLElementL10N[0];
                    self::$instance['arrTranslations'][$antiDuplicatekey][$key] = $value;
                }
            }
        }
        return self::$instance['arrTranslations'][$antiDuplicatekey];
    }
    /**
     * - Get L10N string with htmlentities from $WGF_HOME/repository/widgetPool/wp[Max]/widgetBase/$strProductFolder/lang/[lang].xml
     */    
    public function getLabel($strStringId){
        $assocLabel = $this->getAssocLabel();
        // htmlentities must set "UTF-8", otherwise it will use ISO-8859-1 to encode
        $ret = isset($assocLabel[$strStringId]) ? htmlentities($assocLabel[$strStringId], ENT_QUOTES, "UTF-8") : $strStringId ;
        return $ret;
    }
    /**
     * - Get L10N string without htmlentities from $WGF_HOME/repository/widgetPool/wp[Max]/widgetBase/$strProductFolder/lang/[lang].xml
     */    
    public function getLabelText($strStringId){
        $assocLabel = $this->getAssocLabel();
        $ret = isset($assocLabel[$strStringId]) ? $assocLabel[$strStringId] : $strStringId ;
        return $ret;
    }
}
